﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebBanHang.Models;
using WebBanHang.ViewModel;

namespace WebBanHang.Areas.Admin.Controllers
{
    public class TinTucController : Controller
    {
        private HNCSolutionsEntities db = new HNCSolutionsEntities();

        //
        // GET: /Admin/TinTuc/

        public ActionResult Index(int currentPage = 1, int pageRows = 10)
        {
            var listTinTucs = db.TinTucs.OrderByDescending(sp => sp.NgayViet).Skip((currentPage - 1) * pageRows).Take(pageRows).ToList();
            int totalPages = (db.TinTucs.Count() / pageRows) + (db.TinTucs.Count() % pageRows > 0 ? 1 : 0);
            ListTinTucViewModel viewModel = new ListTinTucViewModel
            {
                ListTinTuc = listTinTucs,
                Paging = new Paging { CurrentPage = currentPage, PageRows = pageRows, TotalPage = totalPages }
            };

            return View(viewModel);
        }

        //
        // GET: /Admin/TinTuc/Details/5

        public ActionResult Details(int id = 0)
        {
            TinTuc tintuc = db.TinTucs.Find(id);
            if (tintuc == null)
            {
                return HttpNotFound();
            }
            return View(tintuc);
        }

        //
        // GET: /Admin/TinTuc/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Admin/TinTuc/Create

        [HttpPost]
        public ActionResult Create(TinTuc tintuc)
        {
            if (ModelState.IsValid)
            {
                var fileName = Path.GetFileName(tintuc.File.FileName);
                tintuc.HinhAnh = fileName;
                var path = Path.Combine(Server.MapPath("~/Images/Upload"), fileName);
                tintuc.File.SaveAs(path);
                db.TinTucs.Add(tintuc);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tintuc);
        }

        //
        // GET: /Admin/TinTuc/Edit/5

        public ActionResult Edit(int id = 0)
        {
            TinTuc tintuc = db.TinTucs.Find(id);
            if (tintuc == null)
            {
                return HttpNotFound();
            }
            return View(tintuc);
        }

        //
        // POST: /Admin/TinTuc/Edit/5

        [HttpPost]
        public ActionResult Edit(TinTuc tintuc)
        {
            if (ModelState.IsValid)
            {
                if (tintuc.File != null)
                {
                    var fileName = Path.GetFileName(tintuc.File.FileName);
                    tintuc.HinhAnh = fileName;
                    var path = Path.Combine(Server.MapPath("~/Images/Upload"), fileName);
                    tintuc.File.SaveAs(path);
                }

                db.Entry(tintuc).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            return View(tintuc);
        }

        public ActionResult Submit(string submitForm, IEnumerable<int> intDeletedIds)
        {
            var tinTucs = this.db.TinTucs.Where(sp => intDeletedIds.Contains(sp.Id)).ToList();
            tinTucs.ForEach(sp => this.db.TinTucs.Remove(sp));
            this.db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}